package com.szzg10.thinking.chapter11;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class ListFeatures {

	public static void main(String[] args) {
		List<Pet> pets = new ArrayList<Pet>();
		pets.add(new Rat());
		pets.add(new Manx());
		pets.add(new Cymric());
		pets.add(new Mutt());
		pets.add(new Pug());
		pets.add(new Cymric());
		pets.add(new Pug());
		System.out.println("1: " + pets);
		
		Hamster hamster = new Hamster();
		pets.add(hamster);
		System.out.println("2: " + pets);
		
		System.out.println("3: " + pets.contains(hamster));
		
		pets.remove(hamster);
		Pet pet = pets.get(2);
		System.out.println("4: " + pet + " " + pets.indexOf(pet));
		
		Pet cymric = new Cymric();
		System.out.println("5: " + pets.indexOf(cymric));
		
		System.out.println("6: " + pets.remove(cymric));
		
		System.out.println("7: " + pets.remove(pet));
		
		System.out.println("8: " + pets);
		
		pets.add(3, new Mouse());
		System.out.println("9: " + pets);
		
		List<Pet> sub = pets.subList(1, 4);
		System.out.println("subList: " + sub);
		
		System.out.println("10: " + pets.containsAll(sub));
		
		Collections.sort(sub);
		System.out.println("sorted subList: " + sub);
		
		System.out.println("11: " + pets.containsAll(sub));
		
		Collections.shuffle(sub, new Random(47));
		System.out.println("shuffled subList: " + sub);
		
		System.out.println("12: " + pets.containsAll(sub));
		
		List<Pet> copy = new ArrayList<Pet>(pets);
		sub = Arrays.asList(pets.get(1), pets.get(4));
		System.out.println("sub: " + sub);
		
		copy.retainAll(sub);
		System.out.println("13: " + copy);

		copy = new ArrayList<Pet>(pets);
		copy.remove(2);
		System.out.println("14: " + copy);
		
		copy.removeAll(sub);
		System.out.println("15: " + copy);
		
		copy.set(1, new Mouse());
		System.out.println("16: " + copy);
		
		copy.addAll(2, sub);
		System.out.println("17: " + copy);
		
		System.out.println("18: " + pets.isEmpty());
		
		pets.clear();
		System.out.println("19: " + pets);
		
		System.out.println("20: " + pets.isEmpty());
		
		Collections.addAll(pets, new Manx(), new Cymric(), new Rat(), new EgyptianMau());
		System.out.println("21: " + pets);
		
		Object[] objects = pets.toArray();
		System.out.println("22: " + objects[3]);
		
		Pet[] pa = pets.toArray(new Pet[0]);
		System.out.println("23: " + pa[3]);
	}
}

class Pet implements Comparable<Pet> {
	@Override
	public String toString() {
		return "Pet";
	}

	@Override
	public int compareTo(Pet o) {
		return toString().compareTo(o.toString());
	}
}

class Rat extends Pet {
	@Override
	public String toString() {
		return "Rat";
	}
}

class Manx extends Pet {
	@Override
	public String toString() {
		return "Manx";
	}
}

class Cymric extends Pet {
	@Override
	public String toString() {
		return "Cymric";
	}
}

class Mutt extends Pet {
	@Override
	public String toString() {
		return "Mutt";
	}
}

class Pug extends Pet {
	@Override
	public String toString() {
		return "Pug";
	}
}

class Hamster extends Pet {
	@Override
	public String toString() {
		return "Hamster";
	}
}

class Mouse extends Pet {
	@Override
	public String toString() {
		return "Mouse";
	}
}

class EgyptianMau extends Pet {
	@Override
	public String toString() {
		return "EgyptianMau";
	}
}

class Cat extends Pet {
	@Override
	public String toString() {
		return "Cat";
	}
}

class Dog extends Pet {
	@Override
	public String toString() {
		return "Dog";
	}
}
